package ytdp.careagle.com.ytdp.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import ytdp.careagle.com.ytdp.activity.WelcomeActivity;
import ytdp.careagle.com.ytdp.app.G;
import ytdp.careagle.com.ytdp.app.manager.LoginManager;
import ytdp.careagle.com.ytdp.entity.BaseConfig;
import ytdp.careagle.com.ytdp.entity.LoginEntity;
import ytdp.careagle.com.ytdp.mvp.mode.CarEvaluateModel;
import ytdp.careagle.com.ytdp.mvp.mode.LoginModel;
import ytdp.careagle.com.ytdp.mvp.view.ILoginView;
import ytdp.careagle.com.ytdp.net.MyObserver;
import ytdp.careagle.com.ytdp.net.Result;
import ytdp.careagle.com.ytdp.utils.MobileCheckUtils;
import ytdp.careagle.com.ytdp.utils.SPUtils;

/**
 * Created by admin on 2017/9/12.
 */

public class LoginPresenter {

    private LoginModel model;
    private ILoginView view;
    private Context context;

    public LoginPresenter(ILoginView view) {
        this.view = view;
        model = new LoginModel();
    }

    public void goLogin(final Context context) {
        this.context = context;
        final String username = view.getUsername();
        final String password = view.getPassword();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            view.showMessage("用户名或密码不能为空!");
            return;
        }
        if (!MobileCheckUtils.isMobileNO(username)) {
            view.showMessage("请输入正确的手机号!");
            return;
        }
        if (password.length() < 6 || password.length() > 16) {
            view.showMessage("请输入6-16位密码!");
            return;
        }
        view.showProgress("正在登录");
        model.goLogin(context, username, password, new MyObserver<Result<LoginEntity>>() {
            @Override
            public void onNext(Result<LoginEntity> loginEntityResult) {
                super.onNext(loginEntityResult);
                if (loginEntityResult.getCode() == G.REQUEST_OK) {
                    SPUtils.put(context, "username", username);
                    SPUtils.put(context, "password", password);
                    LoginManager.loginSuccess(context, loginEntityResult.getData());
                    getBasecfg();
                } else {
                    view.showMessage(loginEntityResult.getMessage());
                }
                view.hideProgress();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.hideProgress();
                view.showMessage("访问不到服务器");
            }
        });
    }

    public void getBasecfg() {
        CarEvaluateModel carEvaluateModel = new CarEvaluateModel();
        carEvaluateModel.getBasicConfig(context, G.getHeader(context), new MyObserver<Result<BaseConfig>>() {
            @Override
            public void onNext(Result<BaseConfig> baseConfigResult) {
                super.onNext(baseConfigResult);
                if (baseConfigResult.getCode() == G.REQUEST_OK) {
                    BaseConfig.DefaultCityBean defaultCity = baseConfigResult.getData().getDefault_city();
                    String extension = defaultCity.getExtension();
                    String value = defaultCity.getValue();
                    if (extension != null) {
                        SPUtils.put(context, "city_extension", extension);
                        SPUtils.put(context, "city_value", value);
                    }
                    String faceExtension = baseConfigResult.getData().getIdentify_remind().getExtension();
                    SPUtils.put(context, "faceExtension", faceExtension);
                }
                view.showLoginSuccess();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                view.showLoginSuccess();
            }
        });
    }
}